//
//  LMRadioController.m
//  EPA
//
//  Created by lanou3g on 16/4/20.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMRadioController.h"
#import "LMRadioListCell.h"
#import "LMNetWorkRequestManager.h"
#import "LMRadioListModel.h"

#include "LMRodioPlayController.h"
#import "UIImage+ImageEffects.h"
@interface LMRadioController ()
///数组
@property (nonatomic, strong) NSMutableArray *radioArray;

@end

@implementation LMRadioController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed:@"ba.jpg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[image applyDarkEffect]] ;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleGray;
    self.radioArray = [NSMutableArray array];
    [self loadDataFormServerRadioId:self.radioId];
    [self.tableView registerNib:[UINib nibWithNibName:@"LMRadioListCell" bundle:nil] forCellReuseIdentifier:@"LMRadioListCell"];
}


#pragma mark - 解析数据

- (void) loadDataFormServerRadioId:(NSString *)ID{
    NSDictionary *params = [NSDictionary dictionary];
    if ([UserInfoManager shareManager].auth) {
        params =@{@"auth":[UserInfoManager shareManager].auth,@"client":@1,@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"radioid":ID,@"version":@"3.0.4"};
    }else{
        params = @{@"auth":@"",@"client":@1,@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"radioid":ID,@"version":@"3.0.4"} ;
    }

    [LMNetWorkRequestManager requestURL:[NSURL URLWithString:RADIODETAILLIST_URL] Method:LM_POST Params:params  Result:^(NSData *data, NSURLResponse *response, NSError *error) {
    
        NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSLog(@"%@",jsonDict);
        NSDictionary *dataDict = jsonDict[@"data"];
        for (NSDictionary *itemDict in dataDict[@"list"]) {
            LMRadioListModel *rModel = [[LMRadioListModel alloc]init];
            [rModel setValuesForKeysWithDictionary:itemDict];
            [self.radioArray addObject:rModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        

}];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.radioArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LMRadioListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LMRadioListCell" forIndexPath:indexPath];
    
    cell.model = self.radioArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LMRodioPlayController *rodioPlayVC = [[ LMRodioPlayController alloc]init];
    rodioPlayVC.radioArray = self.radioArray;
    rodioPlayVC.currentIndex = indexPath.row;
    [self.navigationController pushViewController:rodioPlayVC animated:YES];
    
}











/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
